<template>
  <div class="app-container">
    <!-- 查询 -->
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="85px">
      <el-form-item label="仓库编号" prop="warehouseCode">
        <el-input
          v-model="queryParams.warehouseCode"
          placeholder="请输入仓库编号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item label="仓库名称" prop="warehouseName">
        <el-input
          v-model="queryParams.warehouseName"
          placeholder="请输入仓库名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 按钮 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['crop:warehouse:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['crop:warehouse:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['crop:warehouse:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['crop:warehouse:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <!-- 列表 -->
    <el-table v-loading="loading" :data="warehouseList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="仓库编号" align="center" prop="warehouseCode" />
      <el-table-column label="仓库名称" align="center" prop="warehouseName" />
      <el-table-column label="仓库排序号" align="center" prop="warehouseSort"/>
      <el-table-column label="地址" align="center" prop="address"/>
      <el-table-column label="负责人" align="center" prop="person">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.sys_warehouse_person" :value="scope.row.person"/>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="createTime" width="100"/>
      <el-table-column label="仓库容量" align="center" prop="warehouseCapacity" width="100"/>
      <el-table-column label="剩余容量" align="center" prop="warehouseUsable" width="100"/>
      <el-table-column label="备注" align="center" prop="remark"/>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handCk(scope.row)"
            v-hasPermi="['crop:warehouse:remove']"
          >查看</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['crop:warehouse:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['crop:warehouse:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改仓库对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="600px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="100px">
        <el-form-item label="仓库名称" prop="warehouseName">
          <el-input v-model="form.warehouseName" placeholder="请输入仓库名称" />
        </el-form-item>

        <el-form-item label="仓库排序号" prop="warehouseSort">
          <el-input-number v-model="form.warehouseSort" @change="handleChangeV" :min="1" label="描述文字"></el-input-number>
        </el-form-item>

        <el-form-item label="地址" prop="addresses">
          <el-cascader
            v-model="addresses"
            :options="addressOptions"
            :props="prop"
            @change="handleChange"></el-cascader>
        </el-form-item>

        <el-form-item label="仓库负责人" prop="person">
          <el-select v-model="form.person" placeholder="仓库负责人" clearable>
            <el-option
              v-for="dict in dict.type.sys_warehouse_person"
              :key="dict.value"
              :label="dict.label"
              :value="dict.value"
            />
          </el-select>
        </el-form-item>

        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>

      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>

    <el-drawer
      size="80%"
      title="仓库详情"
      :visible.sync="drawer"
      :direction="direction">
      <el-table v-loading="loading" :data="warehouseData" @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" align="center" />
        <el-table-column label="产品名称" align="center" prop="productName" />
        <el-table-column label="入库时间" align="center" prop="createTime" />
        <el-table-column label="数量" align="center" prop="number" />
        <el-table-column label="来源地" align="center" prop="address" />
      </el-table>
    </el-drawer>


  </div>
</template>

<script>
import { listWarehouse, getWarehouse, delWarehouse, addWarehouse, updateWarehouse,signList} from "@/api/package/stash";
import {cityTreeSelect} from "@/api/crop/products";

export default {
  name: "Stash",
  dicts: ['sys_normal_disable','sys_crop_type','sys_crop_plant','sys_crop_workshop',
    'sys_fertilizer','sys_crop_status','sys_grow_status','sys_stock_type','sys_stock_status','sys_warehouse_person'],
  data() {
    return {
      warehouseData:[],
      direction:'rtl',
      drawer:false,
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 仓库表格数据
      warehouseList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        warehouseCode: undefined,
        warehouseName: undefined,
      },
      // 表单参数
      form: {
        xid: '',
        yid: '',
        zid: '',
        warehouseSort: 1,
      },
      //三级联动
      addresses: [],
      addressOptions: [],
      prop: {
        value: 'id',
        label: 'name',
      },
      // 表单校验
      rules: {
        warehouseName: [
          { required: true, message: "仓库名称不能为空", trigger: "blur" }
        ],
        person: [
          { required: true, message: "仓库负责人不能为空", trigger: "blur" }
        ],
      },
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查看 */
    handCk(row){
      signList(row.warehouseId).then(r=>{
        this.warehouseData=r.data;
      })
      this.drawer=true;
    },
    /** 查询仓库列表 */
    getList() {
      this.loading = true;
      listWarehouse(this.queryParams).then(response => {
        this.warehouseList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },

    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        warehouseName: undefined,
        warehouseSort: 1,
        remark: undefined,
        addresses: undefined,
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.warehouseId)
      this.single = selection.length!=1
      this.multiple = !selection.length
    },

    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.addresses=[];
      this.cityList();
      this.open = true;
      this.title = "添加仓库";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const warehouseId = row.warehouseId || this.ids
      getWarehouse(warehouseId).then(response => {
        this.form = response.data;
        this.cityList();
        //回显地址
        if (this.form.xid!=null){
          this.addresses = [];
          this.addresses.push(this.form.xid+'');
          this.addresses.push(this.form.yid+'');
          this.addresses.push(this.form.zid+'');
        };
        this.open = true;
        this.title = "修改仓库";
      });
    },
    /** 提交按钮 */
    submitForm: function() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.warehouseId != undefined) {
            updateWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addWarehouse(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const warehouseIds = row.warehouseId || this.ids;
      this.$modal.confirm('是否确认删除岗位编号为"' + warehouseIds + '"的数据项？').then(function() {
        return delWarehouse(warehouseIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('crop/warehouse/export', {
        ...this.queryParams
      }, `warehouse_${new Date().getTime()}.xlsx`)
    },

    /** 计数器 */
    handleChangeV(value) {
      console.log(value);
    },

    /** 查询城市列表 */
    cityList(){
      cityTreeSelect().then(r=>{
        this.addressOptions=r.data;
      })
    },

    /** 三级联动 */
    handleChange(value){
      if (value!=null && value.length>0){
        this.form.xid=value[0];
        this.form.yid=value[1];
        this.form.zid=value[2];
      }
    },

  }
};
</script>

<style scoped>

</style>

